# -*- coding: utf-8 -*-
# @Author   : niejiali
# @Time     : 2019/1/17 14:08
# @File     : client.py
# @Software : PyCharm
from app.lib.enums import ClientTypeEnum
from app.lib.error_code import Success
from app.lib.redprint import Redprint
from app.models.user import User
from app.validators.forms import ClientForm, UserEmailForm

api = Redprint('client')


@api.route('/register', methods=['POST'])
def create_client():
    # # 注册 登录
    # # 参数 校验 接收参数
    # # WTForms 验证表单
    # # 表单 json
    # data = request.json
    # data = request.args.to_dict()
    form = ClientForm().validate_for_api()
    promise = {
        ClientTypeEnum.USER_EMAIL: __register_user_by_email
    }
    promise[form.type.data]()
    # 我们可以预知的异常 已知异常
    # 我们没有意识到的异常 未知异常
    # 我们可以接收定义时候的复杂 但不能接受调用时的复杂
    # AOP
    return Success()


def __register_user_by_email():
    form = UserEmailForm().validate_for_api()
    User.register_by_email(form.nickname.data, form.account.data, form.secret.data)
